﻿using Quartz;
using Quartz.Impl;
using Topshelf;

namespace JobBatDeal
{
    /// <summary>
    /// 提供服务操作
    /// </summary>
    public sealed class ServiceRunner : ServiceControl, ServiceSuspend
    {
        private readonly IScheduler _scheduler;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ServiceRunner()
        {
            _scheduler = StdSchedulerFactory.GetDefaultScheduler();
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="hostControl">宿主操作</param>
        /// <returns>是否成功</returns>
        public bool Start(HostControl hostControl)
        {
            _scheduler.Start();
            return true;
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="hostControl">宿主操作</param>
        /// <returns>是否成功</returns>
        public bool Stop(HostControl hostControl)
        {
            _scheduler.Shutdown(false);
            return true;
        }

        /// <summary>
        /// 恢复服务
        /// </summary>
        /// <param name="hostControl">宿主操作</param>
        /// <returns>是否成功</returns>
        public bool Continue(HostControl hostControl)
        {
            _scheduler.ResumeAll();
            return true;
        }

        /// <summary>
        /// 暂停服务
        /// </summary>
        /// <param name="hostControl">宿主操作</param>
        /// <returns>是否成功</returns>
        public bool Pause(HostControl hostControl)
        {
            _scheduler.PauseAll();
            return true;
        }
    }
}